'use strict';
const db = uniCloud.database()

exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ')
	console.log(event)

	const dbName = event['dbName'];
	const pageIndex = event['pageIndex'];
	const pageSize = event['pageSize'];
	const keyword = event['keyword'];
	var key = event['key'];
	const dbCmd = db.command;
	var filterString = "{\"" + key + "\":null}"
	var filter = JSON.parse(filterString)
	filter[key] = new RegExp('.*' + keyword + '.*', 'i')

	//查询数据并返回给前端
	// var res = await db.collection(dbName)
	// 	.where(filter)
	// 	.skip((pageIndex - 1) * pageSize)
	// 	.limit(pageSize)
	// 	.get()
	// console.log(res)
	
	let res = await db.collection(dbName)
		.aggregate()
		.sample({
			size: 4
		})
		.end()
	return res
};